﻿#include "stdafx.h"
#include "XFrameIntParser.h"

#include "XBSTR.h"
#include "XString.h"
#include "StrUtil.h"
#include "Util.h"
#include "IXColorDraw.h"
#include "IXImageDraw.h"
#include "IXFrame.h"

#include "ParseHelper.h"


CXFrameIntParser::CXFrameIntParser() : CXIntParserBase(&TIntCreateControl<IXFrame>)
{
    ;
}

CXFrameIntParser::~CXFrameIntParser()
{
    ;
}

// 填充属性
BOOL CXFrameIntParser::FillAttr(IN IXConfigNode* pNode, IN IXControl* pControl)
{
    BOOL bResult = m_ParentParser.FillAttr(pNode, pControl);
    if(!bResult)
        return bResult;

    XBSTR bstrTemp;
    XString strTemp;
    BOOL bTemp;
    HRESULT hResult;

    IXFrame* pFrame = Util::QueryIntf<IXFrame>(pControl);
    if(pFrame == NULL)
        return FALSE;

    // CURSOR
    hResult = pNode->GetValue(_T("cursor"), &bstrTemp);
    if(SUCCEEDED(hResult))
    {
        struct stCursorInfo
        {
            LPCTSTR szCursorName;
            LPCTSTR szCursorId;
        } data[] =
        {
            {_T("arrow"),    IDC_ARROW},
            {_T("cross"),    IDC_CROSS},
            {_T("hand"),     IDC_HAND},
            {_T("help"),     IDC_HELP},
            {_T("ibeam"),    IDC_IBEAM},
            {_T("no"),       IDC_NO},
            {_T("sizeall"),  IDC_SIZEALL},
            {_T("sizenesw"), IDC_SIZENESW},
            {_T("sizens"),   IDC_SIZENS},
            {_T("sizenwse"), IDC_SIZENWSE},
            {_T("sizewe"),   IDC_SIZEWE},
            {_T("uparrow"),  IDC_UPARROW},
            {_T("wait"),     IDC_WAIT},
        };
        int nIndex = 0;
        for(nIndex=0; nIndex<_countof(data); ++ nIndex)
        {
            if(_tcsicmp(bstrTemp, data[nIndex].szCursorName) == 0)
            {
                break;
            }
        }
        if(nIndex < _countof(data))
        {
            pFrame->SetCursor(data[nIndex].szCursorId);
        }
    }

    // LAYOUT ABLE
    hResult = pNode->GetValue(_T("layoutable"), &bTemp);
    if(SUCCEEDED(hResult))
        pFrame->SetLayoutable(bTemp);

    // LAYOUT TYPE
    hResult = pNode->GetValue(_T("layouttype"), &bstrTemp);
    if(SUCCEEDED(hResult))
    {
        struct stLayoutInfo
        {
            LPCTSTR szLayoutName;
            DWORD   type;
        } data[] =
        {
            {_T("left"),     FLT_Left},
            {_T("right"),    FLT_Right},
            {_T("top"),      FLT_Top},
            {_T("bottom"),   FLT_Bottom},
            {_T("vcenter"),  FLT_VCenter},
            {_T("hcenter"),  FLT_HCenter},
            {_T("vfill"),    FLT_VFill},
            {_T("hfill"),    FLT_HFill},
            {_T("center"),   FLT_HCenter | FLT_VCenter},
        };
        XString strToken;
        XString strText(bstrTemp);
        size_t handle = 0;
        DWORD dwType = 0;
        while(StrUtil::NextToken(strText, handle, _T(";"), strToken, FALSE))
        {
            for(int i=0; i<_countof(data); ++ i)
            {
                if(_tcsicmp(data[i].szLayoutName, strToken.GetData()) == 0)
                {
                    dwType |= data[i].type;
                }
            }
        }
        if(dwType != 0)
            pFrame->SetFrameLayoutType(dwType);
    }

    // DROP
    hResult = pNode->GetValue(_T("drop"), &bTemp);
    if(SUCCEEDED(hResult))
        pFrame->SetDropable(bTemp);

    // TOOLTIP
    hResult = pNode->GetValue(_T("tooltip"), &bstrTemp);
    if(SUCCEEDED(hResult))
    {
        XString strTemp(bstrTemp);
        pFrame->SetTooltip(strTemp.GetData());
    }

    // ShowTooltip
    hResult = pNode->GetValue(_T("showtooltip"), &bTemp);
    if(SUCCEEDED(hResult))
    {
        pFrame->ShowTooltip(bTemp);
    }

    return bResult;
}
